function getXmlHttpObject(){
	var httpRequest=null;
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		httpRequest= new XMLHttpRequest();
	}else if (window.ActiveXObject){
		// code for IE6, IE5
		httpRequest= ActiveXObject("Microsoft.XMLHTTP");
	}
	return httpRequest;
}

var httpRequest;
var elementId;
var privileges=new Array('SELECT', 'INSERT', 'DELETE', 'UPDATE', 'REFERENCES');
var withGrantOption=false;
var privilegesString;

function setPrivilegesList(privString){	
	if(privString){
		privileges=privString.split(",");
		privilegesString=privString;
		withGrantOption=true;
	}
}

function getUserAndGroups(dbname,option){	
	xmlhttp=getXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	elementId="grantTo";
	
	grantToList=document.getElementById(elementId);
	if(option=='P'){		
		grantToList.innerHTML='';
		grantToList.disabled=true;
		return;
	}

	grantToList.disabled=false;
	
	var remotefunction=option=='U'?'getUsers':'getGroups';
	var url="otorgarRevocar.functions.php";
	
	url+="?function="+remotefunction;
	url+="&dbname="+dbname;
	url+="&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);	
}

function getPrivilegesForUser(dbname,tableowner,tablename,user){
	xmlhttp=getXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Your browser does not support AJAX!");
	  return;
	}

	var remotefunction='getPrivileges';
	
	var url="otorgarRevocar.functions.php";
	
	url+="?function="+remotefunction;
	url+="&dbname="+dbname;
	url+="&tableowner="+tableowner;
	url+="&tablename="+tablename;
	url+="&user="+user;
	url+="&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChangedForPrivilegesLists;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);	
	
}

function stateChangedForPrivilegesLists(){	
	if (xmlhttp.readyState==4){
		responseText=xmlhttp.responseText;
		responseText= responseText.replace(/^\s*|\s*$/g,"");
		
		//document.getElementById(elementId).innerHTML=responseText;
		if(responseText.length>0){
			grantedPrivileges=responseText.split(',');
		}else{
			grantedPrivileges=new Array(0);
		}
		
		allprivilegesList=document.getElementById("allPrivilegesList");
		grantedPrivilegesList=document.getElementById("grantedPrivileges");
		

		allprivilegesList.innerHTML='';
		grantedPrivilegesList.innerHTML='';
		
		for(i=0;i<grantedPrivileges.length;i++){	
			privtext=grantedPrivileges[i];
			privvalue=grantedPrivileges[i];
			grantedPrivilegesList.options[i]=new Option(privtext,privvalue);
		}
		
		for(i=0,j=0;i<privileges.length;i++){		
			privilege=privileges[i];
			if(responseText.search(privilege)!=-1){
				continue;
			}
			allprivilegesList.options[j++]=new Option(privilege,privilege);
		}		
	}
}

function stateChangedForRefColumns(){
	if (xmlhttp.readyState==4){
		responseText=xmlhttp.responseText;
		elements=document.getElementsByTagName("select");
		for(i=0;i<elements.length;i++){
			element=elements[i];
			if(element.className==elementId){
				element.innerHTML=responseText;
			}
		}
	}
}

function stateChanged(){
	if (xmlhttp.readyState==4){
		responseText=xmlhttp.responseText;
		//alert(responseText);
		document.getElementById(elementId).innerHTML=responseText;
	}
}

function setAllPrivileges(){
	allPrivileges=document.getElementById("allPrivileges");	
	state=allPrivileges.checked;
	
	allprivilegesList=document.getElementById("allPrivilegesList");
	grantedPrivilegesList=document.getElementById("grantedPrivileges");

	btnAddPrivilege=document.getElementById("btnAddPrivilege");
	btnRemovePrivilege=document.getElementById("btnRemovePrivilege");
	
	allprivilegesList.disabled=state;
	grantedPrivilegesList.disabled=state;
	btnAddPrivilege.disabled=state;
	btnRemovePrivilege.disabled=state;
}

function swapListElementsWithGrantOption(grantedPrivileges,allPrivileges){
	if(withGrantOption){
		for(i=0;i<grantedPrivileges.options.length;i++){
			option=grantedPrivileges.options[i];
			privilege=option.value;
			//alert(privilegesString+" "+privilege+" "+privilegesString.search(privilege));
			if(option.selected&&privilegesString.search(privilege)<0){
				alert('Privilegios insuficientes para revocar el privilegio: '+privilege);
				return
			}		
		}
	}
	swapListElements(grantedPrivileges,allPrivileges);
}

function verificarFormulario(){	
	grantToList=document.getElementById('grantTo');	
	if(grantToList.selectedIndex<0){
		alert('Debe seleccionar un usuario o grupo para la modificación de privilegios');
		return false;
	}		

	allPrivilegesList=document.getElementById("allPrivilegesList");
	grantedPrivilegesList=document.getElementById("grantedPrivileges");
//	if(!allprivilegesList.checked){
//		if(grantedPrivilegesList.options.length<=0){
//			alert('Debe especificar algun privilegio para otorgar');
//			return false;
//		}		
//	}
	
	for(i=0;i<allPrivilegesList.options.length;i++){
		option=allPrivilegesList.options[i];
		option.selected=true;
	}
	
	for(i=0;i<grantedPrivilegesList.options.length;i++){
		option=grantedPrivilegesList.options[i];
		privilege=option.value;
		if(withGrantOption){ 
			privvalue=privilegesString.search(privilege)>=0;
			option.selected=privvalue;
			//alert(option.selected+' '+option.value);
		}else{
			option.selected=true;
		}
	}
	return true;
}